
package com.rt.schedulenew.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.rt.schedulebase.dto.DictScheduleRegularDefDto;
import com.rt.schedulebase.dto.RuleDto;
import com.rt.schedulebase.entity.DictScheduleRegularDef;
import com.rt.schedulenew.api.IDictScheduleRegularDefService;
import com.rt.schedulenew.utils.base.ConstantsUtil;
import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.utils.global.JsonResultUtil;
import com.rt.schedulenew.utils.global.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * 预约规则相关的接口
 */
@RestController
@RequestMapping({"/regular"})
public class RegularController {
    @Autowired
    private IDictScheduleRegularDefService dictScheduleRegularDefService;

    /***
     * 规则定义列表
     * @param dto
     * @return
     */
    @PostMapping({"/def/list"})
    public JsonResult getScheduleRegularList(@RequestBody DictScheduleRegularDefDto dto) {
        if (StringUtil.isEmpty(dto.getExamClass())) {
            return JsonResultUtil.success("成功",new ArrayList<>(),null);
        }
        dto.setRegularModule(ConstantsUtil.RegularModule.REGULAR_SCHEDULE.getKey());
        return getRegularList(dto);
    }

    @PostMapping({"/def/regularInfo"})
    public JsonResult getScheduleRegularInfo(@RequestBody DictScheduleRegularDefDto dto) {
        dto.setRegularModule(ConstantsUtil.RegularModule.REGULAR_SCHEDULE.getKey());
        return dictScheduleRegularDefService.getRegularDefInfo(dto);
    }

    @PostMapping({"/def/delete"})
    public JsonResult delScheduleRegular(@RequestBody DictScheduleRegularDefDto dto) {
        dto.setRegularModule(ConstantsUtil.RegularModule.REGULAR_SCHEDULE.getKey());
        return dictScheduleRegularDefService.delRegularDef(dto);
    }

    @PostMapping({"/def/save"})
    public JsonResult saveScheduleRegular(@RequestBody DictScheduleRegularDefDto dto) {
        dto.setRegularType("0");
        dto.setRegularModule(ConstantsUtil.RegularModule.REGULAR_SCHEDULE.getKey());
        return dictScheduleRegularDefService.saveRegularDef(dto);
    }

    @PostMapping({"/def/edit"})
    public JsonResult editScheduleRegular(@RequestBody DictScheduleRegularDefDto dto) {
        dto.setRegularType("0");
        dto.setRegularModule(ConstantsUtil.RegularModule.REGULAR_SCHEDULE.getKey());
        return dictScheduleRegularDefService.editRegularDef(dto);
    }

    @PostMapping({"/def/initRegularInfo"})
    public JsonResult getInitScheduleRegularInfo(@RequestBody DictScheduleRegularDefDto dto) {
        dto.setRegularModule(ConstantsUtil.RegularModule.REGULAR_SCHEDULE.getKey());
        RuleDto info = dictScheduleRegularDefService.getInitRegularCommonInfo(dto);
        return JsonResultUtil.success(info);
    }

    /**
     * 数量规则列表
     *
     * @param dto
     * @return
     */
    @PostMapping({"/number/list"})
    public JsonResult getNumberRegularList(@RequestBody DictScheduleRegularDefDto dto) {
        dto.setRegularModule(ConstantsUtil.RegularModule.REGULAR_NUMBER.getKey());
        return getRegularList(dto);
    }

    /***
     * 数量规则信息
     * @param dto
     * @return
     */
    @PostMapping({"/number/regularInfo"})
    public JsonResult getNumberRegularInfo(@RequestBody DictScheduleRegularDefDto dto) {
        dto.setRegularModule(ConstantsUtil.RegularModule.REGULAR_NUMBER.getKey());
        return dictScheduleRegularDefService.getRegularDefInfo(dto);
    }

    @PostMapping({"/number/delete"})
    public JsonResult delNumberRegular(@RequestBody DictScheduleRegularDefDto dto) {
        dto.setRegularModule(ConstantsUtil.RegularModule.REGULAR_NUMBER.getKey());
        return dictScheduleRegularDefService.delRegularDef(dto);
    }

    @PostMapping({"/number/save"})
    public JsonResult saveNumberRegular(@RequestBody DictScheduleRegularDefDto dto) {
        dto.setRegularType("3");
        dto.setRegularModule(ConstantsUtil.RegularModule.REGULAR_NUMBER.getKey());
        return dictScheduleRegularDefService.saveRegularDef(dto);
    }

    @PostMapping({"/number/edit"})
    public JsonResult editNumberRegular(@RequestBody DictScheduleRegularDefDto dto) {
        dto.setRegularType("3");
        dto.setRegularModule(ConstantsUtil.RegularModule.REGULAR_NUMBER.getKey());
        return dictScheduleRegularDefService.editRegularDef(dto);
    }

    @PostMapping({"/number/initRegularInfo"})
    public JsonResult getInitNumberRegularInfo(@RequestBody DictScheduleRegularDefDto dto) {
        dto.setRegularModule(ConstantsUtil.RegularModule.REGULAR_NUMBER.getKey());
        return JsonResultUtil.success(dictScheduleRegularDefService.getInitRegularCommonInfo(dto));
    }

    /**
     * 查询预约规则列表
     *
     * @param dto
     * @return
     */
    private JsonResult getRegularList(DictScheduleRegularDefDto dto) {
        Page<?> page = PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        List<DictScheduleRegularDef> list = dictScheduleRegularDefService.selectList(dto);
        PageVO pageVO = new PageVO();
        pageVO.setPageNo(dto.getPageNo());
        pageVO.setPageSize(dto.getPageSize());
        pageVO.setTotal(page.getTotal());
        pageVO.setPages(page.getPages());
        return JsonResultUtil.success("成功", list, pageVO);
    }
}
